﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynaSpace.Entity;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace DynaSpace.Game1
{
	public class BombEntity : UpdatableEntity, IRendeable
	{
		//private static Texture2D texture;
		//private const double TIMETOEXPLODE = 3; // za jak dlouho exploduje?
		//private double secondsToExplode = TIMETOEXPLODE; 
		//private const int BOMBRANGE = 4; // rozsah výbuchu
		//private const float DAMAGE = 100; // zranění hráče
		private static readonly Vector2 rotationOrigin = new Vector2(SIZEX /2,SIZEY);
		private float rotation = 0;
		private Color color = Color.White;
		private float scale = 1;
		private Player owner;

		public BombEntity(Playground p, Vector2 pos, Player owner)
			: base(p, pos)
		{
			this.owner = owner;
			this.Engine = new CollisionEngine();
		}


		//public override void Update(GameTime gameTime)
		//{
		//    if (secondsToExplode < 0)
		//        return;


		//    // padání bomb
		//    Vector2 newPos = Position+ new Vector2(0f,(float)gameTime.ElapsedGameTime.TotalSeconds * 10f);
		//    if (!Parent.Map.CheckCollision(this, newPos))
		//        this.Position = newPos;

		//    secondsToExplode -= gameTime.ElapsedGameTime.TotalSeconds;
			
		//    byte val = MathHelper.ByteSinus((float)secondsToExplode * 4f, 0,255);
		//    color = new Color(255, val, val);

		//    scale = MathHelper.FloatSinus((float)secondsToExplode * 4f, 1f,1.3f);
		//    rotation = MathHelper.FloatSinus((float)secondsToExplode * 2f, -0.1f, 0.1f);
			
			
		//    if (secondsToExplode < 0)
		//    {
		//        EntityBase[] entitiesInRange = Parent.Map.GetCloseEntities(Position.X - BOMBRANGE , Position.Y - BOMBRANGE, BOMBRANGE * 2, BOMBRANGE * 2).ToArray();
		//        foreach (EntityBase entity in entitiesInRange)
		//        {
		//            // smazat blízké entity (zásah výbuchu)
		//            if ((entity.Position - this.Position).Length() <= BOMBRANGE)
		//            {
		//                // pokud je entita Ground a je mozne ji znicit, nechame ji explodovat
		//                if (entity is GroundEntity && (entity as GroundEntity).CanBeDestroyed)
		//                {
		//                    Parent.Map.Add(new ExplosionEntity(Parent, entity));
		//                }
		//                else if (entity is BombEntity && (entity as BombEntity).secondsToExplode > 0) // v blizkosti je bomba, nechame ji explodovat
		//                {
		//                    (entity as BombEntity).secondsToExplode = 0.1;
		//                } else if (entity is Player) // v blizkosti je hrac, zabit ho bombou
		//                {
		//                    (entity as Player).Damage(DAMAGE);
		//                }
		//            }
		//        }

		//        // vybuch teto bomby
		//        Parent.Map.Add(new ExplosionEntity(Parent, this));

		//        secondsToExplode = 0;
		//    }
		//}

		#region IRendeable Members

		public void Render(Vector2 rel, SpriteBatch sb)
		{
			// vykreslit bombu
			sb.Draw(ComponentClass.texBomb, (Position - rel) * SIZE + rotationOrigin,null, color, rotation, rotationOrigin,scale, SpriteEffects.None, 0);
		}

		#endregion
	}
}
